﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.ContactPhone;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.ContactPhone
{
    public class AddContactPhoneHander : IRequestHandler<AddContactPhoneCommand, APIResult<int>>
    {

        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public AddContactPhoneHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 添加联系方式
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(AddContactPhoneCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var model = mapper.Map<ContactPhones>(request);

            result.data = await db.Insertable(model).ExecuteCommandAsync();
            if (result.data != null)
            {
                result.code = APIResultCode.success;
                result.msg = "添加成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "添加失败";
            }
            return await Task.FromResult(result);
        }
    }
}
